package self.hoy.demo.cloud.nacos.order.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * @author 三共小不
 * @date 2020/6/16
 */
@Slf4j
@RestController
public class OrderController {
    @Value("${server.port}")
    private String port;

    @Value("${service-url.nacos-user-service}")
    private String serverUrl;

    @Resource
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Value("${spring.application.name}")
    private String appName;

    private static final String PROVIDER_SERVICE_NAME = "nacos-payment-provider";

    @GetMapping("/echo/app-name")
    public String echoAppName(){
        String url = "http://192.168.87.1:8013/echo/nacos-order-consumer";
        System.out.println("request url:" + url);
        url = restTemplate.getForObject(url, String.class);
        System.out.println(url);
        //使用 LoadBalanceClient 和 RestTemolate 结合的方式来访问
        ServiceInstance serviceInstance = loadBalancerClient.choose(PROVIDER_SERVICE_NAME);
        url = String.format("http://%s:%s/echo/%s", serviceInstance.getHost(), serviceInstance.getPort(), appName);
        System.out.println("request url:" + url);
        return restTemplate.getForObject(url, String.class);
    }

    @GetMapping("/consumer/payment/nacos/{id}")
    public String get(@PathVariable("id") Integer id) {
        String url = serverUrl + "/payment/nacos/" + id;
        System.out.println("request url:" + url);
        String result = restTemplate.getForObject(url, String.class);
        return result;
    }


    @GetMapping("/order/prod/{pid}")
    public String order (@PathVariable("pid") Integer pid){
        log.info(">>客户下单，这时候要调用商品微服务查询商品信息");
        //从nacos中获取服务地址
        //自定义规则实现随机挑选服务
        List<ServiceInstance> instances = discoveryClient.getInstances (PROVIDER_SERVICE_NAME);
        int index = new Random().nextInt (instances.size());
        ServiceInstance serviceInstance = instances.get(index);
        String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort()+ "/payment/nacos/" + pid;
        log.info(">>从nacos中获取到的微服务地址为:" + url);

        //通过restTemplate调用商品微服务
        String result = restTemplate.getForObject(url, String.class);

        log.info(">>商品信息，查询结果:" + result);
        return result;
    }
}
